<?php
/**
 * Created by PhpStorm.
 * User: lenovo
 * Date: 2020/6/23
 * Time: 11:03
 */
namespace App\Process;

use App\Services\ChatService;
use App\Services\MsgService;
use EasySwoole\Component\Process\AbstractProcess;
use EasySwoole\Component\TableManager;
use EasySwoole\EasySwoole\ServerManager;
use SebastianBergmann\CodeCoverage\Report\PHP;
use Swoole\Process;
use EasySwoole\EasySwoole\Config;

/*
 * 同步离线消息
 */

class OfflineMessage extends AbstractProcess
{
    private $isRun = false;
    public function run($arg)
    {
        $newLoginList= TableManager::getInstance()->get(Config::getInstance()->getConf('CHAT.sync_message'));
        // TODO: Implement run() method.
        $this->addTick(1000,function () use($newLoginList){

            if(!$this->isRun){
                $this->isRun = true;
                 while (true){
                    if(empty($newLoginList)){
                        echo "空".PHP_EOL;
                        continue;
                    }
                      foreach ($newLoginList as $loginUser){
                          go(function ()use($loginUser){
                              echo "遍历".$loginUser['user_id'].'的离线消息'.PHP_EOL;
                              #发消息
                              $msgService = new MsgService();
                              $msgService->offLineMessage($loginUser['user_id']);
                          });
                          #移除离线同步表
                          $newLoginList->del($loginUser['user_id']);
                    }
                }
                $this->isRun = false;
            }
        });

    }

    public function onShutDown()
    {
        // TODO: Implement onShutDown() method.
    }

    public function onReceive(string $str, ...$args)
    {
        // TODO: Implement onReceive() method.
    }
}